package com.java.controller;

import com.github.pagehelper.PageInfo;
import com.java.entity.TeacherLevel;
import com.java.service.TeacherLevelService;
import com.java.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class TeacherLevelController extends BaseController{

    @Autowired
    private TeacherLevelService teacherLevelService;

    @PostMapping("teacherLevel/insert")
    public ResultVo insert(@RequestBody TeacherLevel teacherLevel) {
        int rows = teacherLevelService.insert(teacherLevel);
        return toVo(rows);
    }

    @PostMapping("teacherLevel/delete")
    public ResultVo delete(@RequestBody String teacherLevelId) {
        if (!StringUtils.hasText(teacherLevelId)){
            throw new RuntimeException("teacherLevelId is null");
        }
        int rows = teacherLevelService.delete(Integer.valueOf(teacherLevelId));
        return toVo(rows);
    }

    @PostMapping("teacherLevel/update")
    public ResultVo update(@RequestBody TeacherLevel teacherLevel) {
        int rows = teacherLevelService.update(teacherLevel);
        return toVo(rows);
    }

    @PostMapping("teacherLevel/selectOne")
    public ResultVo selectOne(@RequestBody String teacherLevelId) {
        if (!StringUtils.hasText(teacherLevelId)){
            throw new RuntimeException("teacherLevelId is null");
        }
        TeacherLevel teacherLevel = teacherLevelService.selectOne(Integer.valueOf(teacherLevelId));
        return ResultVo.success(teacherLevel);
    }

    @PostMapping("teacherLevel/selectAll")
    public ResultVo selectAll() {
        List<TeacherLevel> list = teacherLevelService.selectAll();
        return ResultVo.success(list);
    }

    @PostMapping("teacherLevel/selectByPage")
    public ResultVo selectByPage(@RequestBody Map<String, Object> map) {
        Integer pageNum = (Integer) map.remove("pageNum");
        Integer pageSize = (Integer) map.remove("pageSize");
        List<TeacherLevel> list = teacherLevelService.selectByPage(pageNum, pageSize);
        PageInfo<TeacherLevel> pageInfo = new PageInfo<>(list);
        return ResultVo.success(pageInfo);
    }

}
